<?php
class Grid
{
    protected $text =  "{failure:true}";
    protected $tip;
    protected $tip_class;

    function Grid(sfWebRequest $request, $tip)
    {
      // Provera prava
      $credentials = new Credentials();
      $razredi = $credentials->getRazrednis();

      if(!in_array($request->getParameter('skola_id'), sfContext::getInstance()->getUser()->getAttribute('skolas')) and !in_array($request->getParameter('razred_id'), $razredi))
        die($credentials->getError());

      $this->tip = $tip;
      $this->tip_class = ucwords($tip);
      // The ext grid script will send  a task field which will specify what it wants to do
      $task = '';
      if ( $request->getParameter('task'))
      {
        $task = $request->getParameter('task');
      }
      switch($task)
      {
        case "LISTING":
            $this->text = $this->getList($request);
            break;
        case "SEARCH":
            $this->text = $this->searchProfil($request);
            break;

        case "SKOLA_LISTING":
            $this->text = $this->getSkolaList($request);
            break;
        case "SKOLA_SEARCH":
            $this->text = $this->searchSkolaProfil($request);
            break;

        case "UPDATEPRES":
            $this->text = $this->updateProfil($request);
            break;
        case "CREATEPRES":
            $this->text = $this->createProfil($request);
            break;
        case "DELETEPRES":
            $this->text = $this->deleteProfil($request);
            break;
        case "DELETEPROF":
            $this->text = $this->deleteProfesor($request);
            break;
        case "PRINT":
            $this->text = $this->printAdresar($request);
            break;
        case "CREATEMESSAGE":
            $this->text = $this->createMessage($request);
            break;
        default:
            $this->text =  "{failure:true}";
            break;
      }

    }

    public function render()
    {
        return $this->text;
    }

  private function getList(sfWebRequest $request)
  {
      $skolas = DbFinder::from('Skola')->select('Id')->where('SkolaId', $request->getParameter('skola_id'))->find();
      $query = DbFinder::from('Profil')->with($this->tip_class)->join($this->tip_class)->with('Skola');
      if($this->tip_class == 'Ucenik')
        $query = $query->leftJoin('Ucenik.RoditeljId', 'Roditelj.Id')->leftJoin('Ucenik.RazredId', 'Razred.Id')->leftJoin('Ucenik.SkolaId', 'Skola.Id')->with('Razred')->with('Skola')->withColumn('Roditelj.ImeMajka', 'ImeMajka')->withColumn('Roditelj.PrezimeMajka', 'PrezimeMajka')->withColumn('Roditelj.ImeStaratelj', 'ImeStaratelj')->withColumn('Roditelj.PrezimeStaratelj', 'PrezimeStaratelj');
      $query = $query->where('Tip', ProfilPeer::$tip[$this->tip]);

      if(!count($skolas))
        $query = $query->where($this->tip_class.'.SkolaId', $request->getParameter('skola_id'));
      else
      {
         $query = $query->where($this->tip_class.'.SkolaId', '=', $request->getParameter('skola_id'), 'skola_1');
         $query = $query->where($this->tip_class.'.SkolaId', 'in', $skolas, 'skola_2');
         $query = $query->combine(array('skola_1', 'skola_2'), 'or');
      }
      if($request->getParameter('query') != '')
        $query->where('Prezime', 'like', '%'.$request->getParameter('query').'%')->orWhere('Ime', 'like', '%'.$request->getParameter('query').'%');
      if($this->tip_class == 'Ucenik')
        $query = $query->orderBy('Ucenik.RedBr')->orderBy('Prezime')->orderBy('Ime');
      else $query = $query->orderBy('Prezime')->orderBy('Ime');

      $profils = $query->offset($request->getParameter('start'))->find($request->getParameter('limit'));

      $query = DbFinder::from('Profil')->where('Tip', ProfilPeer::$tip[$this->tip]);
      if(!count($skolas))
        $query = $query->where($this->tip_class.'.SkolaId', $request->getParameter('skola_id'));
      else
      {
         $query = $query->where($this->tip_class.'.SkolaId', '=', $request->getParameter('skola_id'), 'skola_1');
         $query = $query->where($this->tip_class.'.SkolaId', 'in', $skolas, 'skola_2');
         $query = $query->combine(array('skola_1', 'skola_2'), 'or');
      }
      if($request->getParameter('query') != '')
        $query->where('Prezime', 'like', '%'.$request->getParameter('query').'%')->orWhere('Ime', 'like', '%'.$request->getParameter('query').'%');
      $nbrows = $query->count();

      switch($this->tip)
      {
        case "direktor":
            return $this->jsonDirektor($profils, $nbrows);
            break;
        case "profesor":
            return $this->jsonProfesor($profils, $nbrows);
            break;
        case "ucenik":
            return $this->jsonUcenik($profils, $nbrows);
            break;
        case "roditelj":
            return $this->jsonRoditelj($profils, $nbrows);
            break;
      }
  }

  private function searchProfil(sfWebRequest $request)
  {
      $skolas = DbFinder::from('Skola')->select('Id')->where('SkolaId', $request->getParameter('skola_id'))->find();
      $query = DbFinder::from('Profil')->with($this->tip_class)->join($this->tip_class)->where('Tip', ProfilPeer::$tip[$this->tip]);
      if($this->tip_class == 'Roditelj')
        $query = $query->leftJoin('Roditelj.Id', 'Ucenik.RoditeljId');
      $query = $query->where($this->tip_class.'.SkolaId', '=', $request->getParameter('skola_id'), 'skola_1');
      //if(count($skolas))
      {
         $query = $query->where($this->tip_class.'.SkolaId', 'in', $skolas, 'skola_2');
         $query = $query->combine(array('skola_1', 'skola_2'), 'or');
      }
      $query2 = DbFinder::from('Profil')->where('Tip', ProfilPeer::$tip[$this->tip]);
      $query2 = $query2->where($this->tip_class.'.SkolaId', '=', $request->getParameter('skola_id'), 'skola_1');
      //if(count($skolas))
      {
         $query2 = $query2->where($this->tip_class.'.SkolaId', 'in', $skolas, 'skola_2');
         $query2 = $query2->combine(array('skola_1', 'skola_2'), 'or');
      }

      if($request->getParameter('prezime') != '')
      {
        $query = $query->where('Prezime', 'like', '%'.$request->getParameter('prezime').'%');
        $query2 = $query2->where('Prezime', 'like', '%'.$request->getParameter('prezime').'%');
      }
      if($request->getParameter('ime') != '')
      {
        $query = $query->where('Ime', 'like', '%'.$request->getParameter('ime').'%');
        $query2 = $query2->where('Ime', 'like', '%'.$request->getParameter('ime').'%');
      }
      if($request->getParameter('razred_id') != '')
      {
        $query = $query->join('Ucenik')->where('Ucenik.RazredId', $request->getParameter('razred_id'));
        $query2 = $query2->join('Ucenik')->where('Ucenik.RazredId', $request->getParameter('razred_id'));
      }
      if($this->tip_class != 'Profesor')
        $query = $query->orderBy('Ucenik.RedBr');
      $query = $query->orderBy('Prezime')->orderBy('Ime');
      $profils = $query->find();
      $nbrows = $query2->count();


      switch($this->tip)
      {
        case "direktor":
            return $this->jsonDirektor($profils, $nbrows);
            break;
        case "profesor":
            return $this->jsonProfesor($profils, $nbrows);
            break;
        case "ucenik":
            return $this->jsonUcenik($profils, $nbrows);
            break;
        case "roditelj":
            return $this->jsonRoditelj($profils, $nbrows);
            break;
      }
  }

  private function getSkolaList(sfWebRequest $request)
  {
      if($this->tip_class == 'Ucenik')
      {
        $query = DbFinder::from('Profil')->with($this->tip_class)->join($this->tip_class)->leftJoin('Ucenik.RazredId', 'Razred.Id')->leftJoin('Ucenik.SkolaId', 'Skola.Id')->with('Razred')->with('Skola')->where('Tip', ProfilPeer::$tip[$this->tip]);
        $query = $query->where($this->tip_class.'.RazredId', $request->getParameter('razred_id'))->orderBy('Ucenik.RedBr');

        $query_2 = DbFinder::from('Profil')->where('Tip', ProfilPeer::$tip[$this->tip])->where($this->tip_class.'.RazredId', $request->getParameter('razred_id'));
        if($request->getParameter('query') != '')
          $query_2->where('Prezime', 'like', '%'.$request->getParameter('query').'%')->orWhere('Ime', 'like', '%'.$request->getParameter('query').'%');
        $nbrows = $query_2->count();
      }
      else if ($this->tip_class == 'Roditelj')
      {
        $query = DbFinder::from('Profil')->with($this->tip_class)->join($this->tip_class)->leftJoin('Roditelj.Id', 'Ucenik.RoditeljId')->leftJoin('Roditelj.SkolaId', 'Skola.Id')->with('Skola')->where('Profil.Tip', ProfilPeer::$tip[$this->tip]);
        $query = $query->where('Ucenik.RazredId', $request->getParameter('razred_id'))->orderBy('Ucenik.RedBr');

        $query_2 = DbFinder::from('Profil')->join($this->tip_class)->leftJoin('Roditelj.Id', 'Ucenik.RoditeljId')->where('Tip', ProfilPeer::$tip[$this->tip])->where('Ucenik.RazredId', $request->getParameter('razred_id'));
        if($request->getParameter('query') != '')
          $query_2->where('Prezime', 'like', '%'.$request->getParameter('query').'%')->orWhere('Ime', 'like', '%'.$request->getParameter('query').'%');
        $nbrows = $query_2->count();
      }
      if($request->getParameter('query') != '')
        $query->where('Prezime', 'like', '%'.$request->getParameter('query').'%')->orWhere('Ime', 'like', '%'.$request->getParameter('query').'%');
      $profils = $query->offset($request->getParameter('start'))->find($request->getParameter('limit'));

      switch($this->tip)
      {
        case "ucenik":
            return $this->jsonUcenik($profils, $nbrows);
            break;
        case "roditelj":
            return $this->jsonRoditelj($profils, $nbrows);
            break;
      }
  }

  private function searchSkolaProfil(sfWebRequest $request)
  {
      $query = DbFinder::from('Profil')->with($this->tip_class)->join($this->tip_class)->where('Tip', ProfilPeer::$tip[$this->tip]);
      if($this->tip_class == 'Roditelj')
        $query = $query->leftJoin('Roditelj.Id', 'Ucenik.RoditeljId')->where('Ucenik.RazredId', $request->getParameter('razred_id'));
      else
        $query = $query->where($this->tip_class.'.RazredId', $request->getParameter('razred_id'));

      if($request->getParameter('prezime') != '')
        $query->where('Prezime', 'like', '%'.$request->getParameter('prezime').'%');
      if($request->getParameter('ime') != '')
        $query->where('Ime', 'like', '%'.$request->getParameter('ime').'%');
      $profils = $query->find();

      switch($this->tip)
      {
        case "ucenik":
            return $this->jsonUcenik($profils);
            break;
        case "roditelj":
            return $this->jsonRoditelj($profils);
            break;
      }
  }

  private function updateProfil(sfWebRequest $request)
  {
      $set_array = array(
                    'Prezime' => $request->getParameter('prezime'),
                    'Ime' => $request->getParameter('ime'),
                    'EMail' => $request->getParameter('email'),
                    'Username' => $request->getParameter('username'),
                    'PlainPassword' => $request->getParameter('password'),
                    'Password' => md5($request->getParameter('password')),
                    'Pol' => $request->getParameter('pol'),
                    'Adresa' => $request->getParameter('adresa'),
                    'Telefon' => $request->getParameter('telefon'),
                    'Mobilni' => $request->getParameter('mobilni'));
      $update_1 = DbFinder::from('Profil')->where('Id', $request->getParameter('IDprofil'))->set($set_array);

      switch($this->tip)
      {
        case "direktor":
            $update_2 = $this->updateDirektor($request);
            break;
        case "profesor":
            $update_2 = $this->updateProfesor($request);
            break;
        case "ucenik":
            $update_2 = $this->updateUcenik($request);
            break;
        case "roditelj":
            $update_2 = $this->updateRoditelj($request);
            break;
      }

      return (($update_1 > 0 or $update_2 > 0) ? 1 : 0);
  }

  private function createProfil(sfWebRequest $request)
  {
      if($this->tip == "ucenik" and $request->getParameter('ucenik_id'))
        return $this->editUcenik($request);

      $con = Propel::getConnection();
      try
      {
         $con->beginTransaction();

         $profil = new Profil();
         $profil->setPrezime($request->getParameter('prezime'));
         $profil->setIme($request->getParameter('ime'));
         $profil->setPol($request->getParameter('pol'));
         $profil->setAdresa($request->getParameter('adresa'));
         $profil->setTelefon($request->getParameter('telefon'));
         $profil->setMobilni($request->getParameter('mobilni'));
         $profil->setTip(ProfilPeer::$tip[$this->tip]);

         if($this->tip != 'ucenik')
         {
           $profil->setEMail($request->getParameter('email'));
           $profil->setUsername($request->getParameter('username'));
           $profil->setPlainPassword($request->getParameter('password'));
         }

         $tst_1 = $profil->save();

         switch($this->tip)
         {
           case "direktor":
               $tst_2 = $this->createDirektor($request, $profil);
               break;
           case "profesor":
               $tst_2 = $this->createProfesor($request, $profil);
               break;
           case "ucenik":
               $tst_2 = $this->createUcenik($request, $profil);
               break;
           case "roditelj":
               $tst_2 = $this->createRoditelj($request, $profil);
               break;
         }

         $con->commit();
      }
      catch(Exception $e)
      {
         $con->rollBack();
         throw $e;
      }

      return ($tst_1 * $tst_2) ? 1 : 0;
  }

  private function deleteProfil(sfWebRequest $request)
  {
      $tst = 0;
      $idpres = json_decode(stripslashes($request->getParameter('ids')));
      foreach($idpres as $id)
        $tst += UcenikPeer::retrieveByPK($id)->delete();

      if($tst > 0)
        return 1;
      else return 0;
  }

  private function deleteProfesor(sfWebRequest $request)
  {
      $tst = 0;
      $idpres = json_decode(stripslashes($request->getParameter('ids')));
      foreach($idpres as $id)
        $tst += ProfesorPeer::retrieveByPK($id)->delete();

      if($tst > 0)
        return 1;
      else return 0;
  }

  // DIREKTOR

  private function jsonDirektor($profils, $nbrows)
  {
      $arr = array();
      foreach($profils as $profil)
      {
        $rec['IDprofil'] = $profil->getId();
        $rec['prezime'] = $profil->getPrezime();
        $rec['ime'] = $profil->getIme();
        $rec['email'] = $profil->getEMail();
        $rec['username'] = $profil->getUsername();
        $rec['password'] = $profil->getPlainPassword();
        $rec['pol'] = $profil->getPol();
        $rec['adresa'] = $profil->getAdresa();
        $rec['telefon'] = $profil->getTelefon();
        $rec['mobilni'] = $profil->getMobilni();


        $rec['skola'] = (string) $profil->getDirektor()->getSkola();
        if($profil->getDirektor()->getTip())
          $rec['tip'] = array_search($profil->getDirektor()->getTip(), DirektorPeer::$tip);
        else
          $rec['tip'] = 0;

        $arr[] = $rec;
      }
      $jsonresult = json_encode($arr);
      return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
  }

  private function updateDirektor(sfWebRequest $request)
  {
      $update_2 = 0;
      $set_array_2 = array();
      if(is_numeric($request->getParameter('tip')))
        $set_array_2['Tip'] = $request->getParameter('tip');
      if(is_numeric($request->getParameter('skola')))
        $set_array_2['SkolaId'] = $request->getParameter('skola');

      if(count($set_array_2))
        $update_2 = DbFinder::from('Direktor')->where('Id', $request->getParameter('IDprofil'))->set($set_array_2);

      return $update_2;

  }

  private function createDirektor(sfWebRequest $request, $profil)
  {
      $direktor = new Direktor();
      $direktor->setId($profil->getId());
      if($request->getParameter('tip'))
        $direktor->setTip($request->getParameter('tip'));
      $direktor->setSkolaId($request->getParameter('skola'));
      $tst_2 = $direktor->save();

      return $tst_2;
  }

  // PROFESOR
  private function jsonProfesor($profils, $nbrows)
  {
      $arr = array();
      foreach($profils as $profil)
      {
        $rec['IDprofil'] = $profil->getId();
        $rec['prezime'] = $profil->getPrezime();
        $rec['ime'] = $profil->getIme();
        $rec['email'] = $profil->getEMail();
        $rec['username'] = $profil->getUsername();
        /*$rec['password'] = $profil->getPlainPassword();*/
        $rec['pol'] = $profil->getPol();
        $rec['adresa'] = $profil->getAdresa();
        $rec['telefon'] = $profil->getTelefon();
        $rec['mobilni'] = $profil->getMobilni();

        $rec['skola'] = (string) $profil->getProfesor()->getSkola();

        $arr[] = $rec;
      }
      $jsonresult = json_encode($arr);
      return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
  }

  private function updateProfesor(sfWebRequest $request)
  {
      $update_2 = 0;
      $set_array_2 = array();
      if(is_numeric($request->getParameter('skola')))
        $set_array_2['SkolaId'] = $request->getParameter('skola');

      if(count($set_array_2))
        $update_2 = DbFinder::from('Profesor')->where('Id', $request->getParameter('IDprofil'))->set($set_array_2);

      return $update_2;

  }

  private function createProfesor(sfWebRequest $request, $profil)
  {
      $profesor = new Profesor();
      $profesor->setId($profil->getId());
      $profesor->setSkolaId($request->getParameter('skola'));
      $tst_2 = $profesor->save();

      return $tst_2;
  }

  // UCENIK
  private function jsonUcenik($profils, $nbrows)
  {
      $arr = array();
      foreach($profils as $profil)
      {
        $rec['IDprofil'] = $profil->getId();
        $rec['prezime'] = $profil->getPrezime();
        $rec['ime'] = $profil->getIme();
        $rec['email'] = $profil->getEMail();
        $rec['username'] = $profil->getUsername();
        $rec['password'] = $profil->getPlainPassword();
        $rec['pol'] = $profil->getPol();
        $rec['adresa'] = $profil->getAdresa();
        $rec['telefon'] = $profil->getTelefon();
        $rec['mobilni'] = $profil->getMobilni();

        $rec['skola'] = (string) $profil->getUcenik()->getSkola();
        $rec['razred'] = (string) $profil->getUcenik()->getRazred();
        $rec['roditelj'] = $this->getRoditeljIme($profil);
        $rec['red_br'] = $profil->getUcenik()->getRedBr();
        $rec['jmbg'] = $profil->getUcenik()->getJmbg();
        $rec['drzava_rod'] = $profil->getUcenik()->getDrzavaRod();
        $rec['mesto_rod'] = $profil->getUcenik()->getMestoRod();
        $rec['opstina_rod'] = $profil->getUcenik()->getOpstinaRod();
        $rec['datum_rod'] = $profil->getUcenik()->getDatumRod();

        $arr[] = $rec;
      }
      $jsonresult = json_encode($arr);
      return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
  }

  private static function getRoditeljIme($profil_uc)
  {
     $profil = DbFinder::from('Profil')->join('Roditelj')->withColumn('Roditelj.ImeMajka', 'ImeMajka')->withColumn('Roditelj.PrezimeMajka', 'PrezimeMajka')->withColumn('Roditelj.ImeStaratelj', 'ImeStaratelj')->withColumn('Roditelj.PrezimeStaratelj', 'PrezimeStaratelj')->where('Profil.Id', $profil_uc->getUcenik()->getRoditeljId())->findOne();
     if($profil->getIme())
        return $profil->getPrezime().' '.$profil->getIme();
     if($profil->getColumn('ImeMajka'))
        return $profil->getColumn('PrezimeMajka').' '.$profil->getColumn('ImeMajka');
     if($profil->getColumn('ImeStaratelj'))
        return $profil->getColumn('PrezimeStaratelj').' '.$profil->getColumn('ImeStaratelj');
  }

  private function updateUcenik(sfWebRequest $request)
  {
      $update_2 = 0;
      $set_array_2 = array();
      if(is_numeric($request->getParameter('skola')))
        $set_array_2['SkolaId'] = $request->getParameter('skola');
      if(is_numeric($request->getParameter('razred')))
        $set_array_2['RazredId'] = $request->getParameter('razred');
      if(is_numeric($request->getParameter('roditelj')))
        $set_array_2['RoditeljId'] = $request->getParameter('roditelj');

      $set_array_2['RedBr'] = $request->getParameter('red_br');
      $set_array_2['Jmbg'] = $request->getParameter('jmbg');
      $set_array_2['DrzavaRod'] = $request->getParameter('drzava_rod');
      $set_array_2['MestoRod'] = $request->getParameter('mesto_rod');
      $set_array_2['OpstinaRod'] = $request->getParameter('opstina_rod');

      if($request->getParameter('datum_rod') != '')
      {
        $datum_array = explode('.', $request->getParameter('datum_rod'));
        $set_array_2['DatumRod'] = mktime(2, 0, 0, $datum_array[1], $datum_array[0], $datum_array[2]);
      }

      if(count($set_array_2))
        $update_2 = DbFinder::from('Ucenik')->where('Id', $request->getParameter('IDprofil'))->set($set_array_2);

      return $update_2;

  }

  private function createUcenik(sfWebRequest $request, $profil)
  {
      $skola_id = DbFinder::from('Razred')->findPk($request->getParameter('razred'))->getSkolaId();
      $roditelj_profil = new Profil();
      $roditelj_profil->setPrezime($request->getParameter('prezime_otac'));
      $roditelj_profil->setIme($request->getParameter('ime_otac'));
      $roditelj_profil->setEMail($request->getParameter('email'));
      $roditelj_profil->setUsername($request->getParameter('username'));
      $roditelj_profil->setPlainPassword($request->getParameter('password'));
      $roditelj_profil->setAdresa($request->getParameter('adresa'));
      $roditelj_profil->setTelefon($request->getParameter('telefon'));
      $roditelj_profil->setMobilni($request->getParameter('mobilni_1'));
      $roditelj_profil->setTip(ProfilPeer::$tip['roditelj']);

      $tst_rod_1 = $roditelj_profil->save();

      $roditelj = new Roditelj();
      $roditelj->setId($roditelj_profil->getId());
      $roditelj->setOcene(1); $roditelj->setDomaci(1); $roditelj->setIzostanci(1); $roditelj->setObavestenja(1);
      $roditelj->setImeMajka($request->getParameter('ime_majka'));
      $roditelj->setPrezimeMajka($request->getParameter('prezime_majka'));
      $roditelj->setImeStaratelj($request->getParameter('ime_staratelj'));
      $roditelj->setPrezimeStaratelj($request->getParameter('prezime_staratelj'));
      $roditelj->setMobilni2($request->getParameter('mobilni_2'));
      $roditelj->setSkolaId($skola_id);
      $tst_rod_2 = $roditelj->save();

      $ucenik = new Ucenik();
      $ucenik->setId($profil->getId());
      $ucenik->setRoditeljId($roditelj_profil->getId());
      $ucenik->setSkolaId($skola_id);
      $ucenik->setRedBr($request->getParameter('red_br'));
      $ucenik->setRazredId($request->getParameter('razred'));
      $ucenik->setJmbg($request->getParameter('jmbg'));
      $ucenik->setDatumRod($request->getParameter('datum_rod'));
      $ucenik->setMestoRod($request->getParameter('mesto_rod'));
      $ucenik->setDrzavaRod($request->getParameter('drzava_rod'));
      $ucenik->setOpstinaRod($request->getParameter('opstina_rod'));
      $tst_2 = $ucenik->save();

      return $tst_rod_1 * $tst_rod_2 * $tst_2;
  }

  private function editUcenik(sfWebRequest $request)
  {
     $update = 0;
     $set_array_1 = array();
     $set_array_1_1 = array();
     $set_array_2 = array();
     $set_array_2_2 = array();

     $set_array_1['RedBr'] = $request->getParameter('red_br');
     $set_array_1['Jmbg'] = $request->getParameter('jmbg');
     $set_array_1['DrzavaRod'] = $request->getParameter('drzava_rod');
     if($request->getParameter('datum_rod') != '')
       $set_array_1['DatumRod'] = $request->getParameter('datum_rod');
     $set_array_1['MestoRod'] = $request->getParameter('mesto_rod');
     $set_array_1['OpstinaRod'] = $request->getParameter('opstina_rod');
     $set_array_1['RazredId'] = $request->getParameter('razred');

     $set_array_1_1['Pol'] = $request->getParameter('pol');
     $set_array_1_1['Adresa'] = $request->getParameter('adresa');
     $set_array_1_1['Telefon'] = $request->getParameter('telefon');
     $set_array_1_1['Mobilni'] = $request->getParameter('mobilni');
     $set_array_1_1['Ime'] = $request->getParameter('ime');
     $set_array_1_1['Prezime'] = $request->getParameter('prezime');

     $set_array_2_2['Username'] = $request->getParameter('username');
     $set_array_2_2['PlainPassword'] = $request->getParameter('password');
     $set_array_2_2['Password'] = md5($request->getParameter('password'));
     $set_array_2_2['Mobilni'] = $request->getParameter('mobilni_1');
     $set_array_2_2['EMail'] = $request->getParameter('email');
     $set_array_2_2['Ime'] = $request->getParameter('ime_otac');
     $set_array_2_2['Prezime'] = $request->getParameter('prezime_otac');

     $set_array_2['ImeMajka'] = $request->getParameter('ime_majka');
     $set_array_2['PrezimeMajka'] = $request->getParameter('prezime_majka');
     $set_array_2['ImeStaratelj'] = $request->getParameter('ime_staratelj');
     $set_array_2['PrezimeStaratelj'] = $request->getParameter('prezime_staratelj');
     $set_array_2['Mobilni2'] = $request->getParameter('mobilni_2');

     $update_1 = DbFinder::from('Ucenik')->where('Id', $request->getParameter('ucenik_id'))->set($set_array_1);
     $update_1_1 = DbFinder::from('Profil')->where('Id', $request->getParameter('ucenik_id'))->set($set_array_1_1);
     $update_2 = DbFinder::from('Roditelj')->where('Id', DbFinder::from('Ucenik')->select('RoditeljId')->findPk($request->getParameter('ucenik_id')))->set($set_array_2);
     $update_2_2 = DbFinder::from('Profil')->where('Id', DbFinder::from('Ucenik')->select('RoditeljId')->findPk($request->getParameter('ucenik_id')))->set($set_array_2_2);

     return ($update_1 || $update_1_1 || $update_2 || $update_2_2 ) ? 1 : 0;
  }

  // RODITELJ
  private function jsonRoditelj($profils, $nbrows)
  {
      $arr = array();
      foreach($profils as $profil)
      {
        $uceniks = ' ';
        $ucenik_dbs = DbFinder::from('Profil')->leftJoin('Profil.Id', 'Ucenik.Id')->leftJoin('Ucenik.RazredId', 'Razred.Id')->with('Ucenik')->with('Razred')->where('Ucenik.RoditeljId', $profil->getId())->find();
        foreach($ucenik_dbs as $ucenik_db)
          $uceniks .= $ucenik_db->getPrezime()." ".$ucenik_db->getIme().' - '.(string) $ucenik_db->getUcenik()->getRazred().", ";
        $rec['ucenici'] = substr($uceniks, 0, strlen($uceniks) - 2);
        $rec['IDprofil'] = $profil->getId();
        $rec['prezime'] = $profil->getPrezime();
        $rec['ime'] = $profil->getIme();
        $rec['email'] = $profil->getEMail();
        $rec['username'] = $profil->getUsername();
        $rec['password'] = $profil->getPlainPassword();
        $rec['pol'] = $profil->getPol();
        $rec['adresa'] = $profil->getAdresa();
        $rec['telefon'] = $profil->getTelefon();
        $rec['mobilni'] = $profil->getMobilni();

        $roditelj = $profil->getRoditelj();
        $rec['skola'] = (string) $roditelj->getSkola();
        $rec['ime_majka'] =  $roditelj->getImeMajka();
        $rec['prezime_majka'] =  $roditelj->getPrezimeMajka();
        $rec['ime_staratelj'] =  $roditelj->getImeStaratelj();
        $rec['prezime_staratelj'] =  $roditelj->getPrezimeStaratelj();
        $rec['mobilni_2'] = (string) $roditelj->getMobilni2();
        $rec['ocene'] = (string) $roditelj->getOcene();
        $rec['izostanci'] = (string) $roditelj->getIzostanci();
        $rec['domaci'] = (string) $roditelj->getDomaci();
        $rec['obavestenja'] = (string) $roditelj->getObavestenja();
        $rec['oba_telefona'] = (string) $roditelj->getObaTelefona();

        $arr[] = $rec;
      }
      $jsonresult = json_encode($arr);
      return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
  }

  private function updateRoditelj(sfWebRequest $request)
  {
      $update_2 = 0;
      $set_array_2 = array();

      $set_array_2['ImeMajka'] = $request->getParameter('ime_majka');
      $set_array_2['PrezimeMajka'] = $request->getParameter('prezime_majka');
      $set_array_2['ImeStaratelj'] = $request->getParameter('ime_staratelj');
      $set_array_2['PrezimeStaratelj'] = $request->getParameter('prezime_staratelj');
      $set_array_2['Mobilni2'] = $request->getParameter('mobilni_2');

      if($request->getParameter('ocene') == 'true')
        $set_array_2['Ocene'] = 1;
      else if($request->getParameter('ocene') == 'false')
        $set_array_2['Ocene'] = 0;
      if($request->getParameter('izostanci') == 'true')
        $set_array_2['Izostanci'] = 1;
      else if($request->getParameter('izostanci') == 'false')
        $set_array_2['Izostanci'] = 0;
      if($request->getParameter('domaci') == 'true')
        $set_array_2['Domaci'] = 1;
      else if($request->getParameter('domaci') == 'false')
        $set_array_2['Domaci'] = 0;
      if($request->getParameter('obavestenja') == 'true')
        $set_array_2['Obavestenja'] = 1;
      else if($request->getParameter('obavestenja') == 'false')
        $set_array_2['Obavestenja'] = 0;
      if($request->getParameter('oba_telefona') == 'true')
        $set_array_2['ObaTelefona'] = 1;
      else if($request->getParameter('oba_telefona') == 'false')
        $set_array_2['ObaTelefona'] = 0;

      if(is_numeric($request->getParameter('skola')))
        $set_array_2['SkolaId'] = $request->getParameter('skola');

      if(count($set_array_2))
        $update_2 = DbFinder::from('Roditelj')->where('Id', $request->getParameter('IDprofil'))->set($set_array_2);

      return $update_2;

  }

  private function createRoditelj(sfWebRequest $request, $profil)
  {
      $roditelj = new Roditelj();
      $roditelj->setId($profil->getId());
      $roditelj->setSkolaId($request->getParameter('skola'));
      $roditelj->setImeMajka($request->getParameter('ime_majka'));
      $roditelj->setPrezimeMajka($request->getParameter('prezime_majka'));
      $roditelj->setImeStaratelj($request->getParameter('ime_staratelj'));
      $roditelj->setPrezimeStaratelj($request->getParameter('prezime_staratelj'));
      $roditelj->setMobilni2($request->getParameter('mobilni_2'));
      $roditelj->setOcene($request->getParameter('ocene'));
      $roditelj->setIzostanci($request->getParameter('izostanci'));
      $roditelj->setDomaci($request->getParameter('domaci'));
      $roditelj->setObavestenja($request->getParameter('obavestenja'));
      $tst_2 = $roditelj->save();

      return $tst_2;
  }

}